#_*_ encoding:utf-8 _*_

# md5方法
def md5(name):
    import hashlib
    hasher = hashlib.md5()
    hasher.update(name)
    return hasher.hexdigest()

# 获取ip
def get_ip(request):
    if request.META.has_key('HTTP_X_FORWARDED_FOR'):
        ip =  request.META['HTTP_X_FORWARDED_FOR']
    else:
        ip = request.META['REMOTE_ADDR']
    return ip

# 获取mac地址
def get_mac_address():
    import uuid
    mac = uuid.UUID(int=uuid.getnode()).hex[-12:]
    return ':'.join([mac[e:e+2] for e in range(0, 11, 2)])

# 只包含中文
def is_all_zh(s):
    for c in unicode(s):
        if not (u'\u4e00' <= c <= u'\u9fa5'):
            return False
    return True

# int转中文数字(unit表示是否要带上单位)
def int_to_chinese(num,unit=True):
    str_num = str(num)
    chinese = {'1': u'一', '2': u'二', '3': u'三', '4': u'四', '5': u'五', '6': u'六', '7': u'七',
        '8': u'八', '9': u'九', '0': u'〇'}
    if not unit:
        formal_list = [chinese[i] for i in str_num]
    else:
        formal_list = []
        for i,n in enumerate(reversed(str_num)):
            if i == 0:
                formal_list.insert(0,chinese[n])
            elif i == 1:
                formal_list.insert(0,chinese[n]+u'十')
            elif i == 2:
                formal_list.insert(0,chinese[n]+u'百')
            elif i == 3:
                formal_list.insert(0,chinese[n]+u'千')
            elif i == 4:
                formal_list.insert(0,chinese[n]+u'万')
    return ''.join(formal_list)

# 通用编码处理
def normal_text(text):
    import sys
    try:
        if sys.getdefaultencoding() == 'ascii':
            text.encode('raw_unicode_escape').encode('utf8')
        elif sys.getdefaultencoding() == 'utf-8':
            text.decode('utf8').encode('gbk')
    except:
        try:
            text = text.encode('raw_unicode_escape').decode('utf8')
        except Exception as e:
            text = text
    return text

# 重写剔除空白字符方法
def clear_content(text,prepare_type=1):
    if type(text) == list:
        if len(text):
            text = text[0]
        else:
            text = ''
    text = text.replace(' ','').replace('\f','').replace('\r','').replace('\n','').replace(u'\xa0','')
    return text
